﻿/*
designed by:tiansx
create date:2014.01.21
update date:2014.01.21
based on: bmap api,ejs
*/
(function ($) {
    $.fn.showMap = function (opts) {
        var defaults = {
            lat: 39.943146, //纬度
            lng: 116.337284, //经度
            data: {}, //图层数据
            zoom: 13, //地图缩放级别
            centerOffset: { lat: 0, lng: 0 }, //中心点偏移量
            NavigationControl: true, //是否添加缩放控件
            OverviewMapControl: true, //是否添加右下角缩略图
            MapTypeControl: true, //是否添加卫星三维图
            label: {
                url: "",
                content: "",
                offset: { w: 0, h: 0 }
            }, //标注内容(可以是url或者字符串)
            icon: {
                url: "http://img4.bitauto.com/dealer/dealersite/20140626/images/map_i24.png",
                size: { w: 28, h: 35 },
                anchor: { w: 14, h: 33 },
                events: [{ name: "click", fn: function () { $(".maplayer").show(); } }]
            }, //自定义标点信息 
            callback: undefined
        };
        var options = $.extend(true, defaults, opts);
        if (typeof (BMap) != "undefined") {
            var map = new BMap.Map(this.attr("id"));
            var point = new BMap.Point(options.lng, options.lat);
            var marker;
            map.centerAndZoom(new BMap.Point(options.lng + options.centerOffset.lng, options.lat + options.centerOffset.lat), options.zoom);
            
            map.enableScrollWheelZoom();
            if (options.NavigationControl) {
                map.addControl(new BMap.NavigationControl());
            }
            if (options.OverviewMapControl) {
                map.addControl(new BMap.OverviewMapControl({ anchor: BMAP_ANCHOR_BOTTOM_RIGHT, isOpen: true }));
            }
            if (options.MapTypeControl) {
                map.addControl(new BMap.MapTypeControl());
            }
            
            if (options.icon.url != "") {
                var myIcon = new BMap.Icon(options.icon.url, new BMap.Size(options.icon.size.w, options.icon.size.h), {
                    anchor: new BMap.Size(options.icon.anchor.w, options.icon.anchor.h),
                    imageOffset: new BMap.Size(0, 0)   // 设置图片偏移
                });
                marker = new BMap.Marker(point, { icon: myIcon });
                map.addOverlay(marker);
            } else {
                marker = new BMap.Marker(point);
                map.addOverlay(marker);
            }

            if (options.icon.events) {
                for (var i = 0; i < options.icon.events.length; i++) {
                    marker.addEventListener(options.icon.events[i].name, options.icon.events[i].fn);
                }
            }

            if (options.label.url != "" || options.label.content != "") {
                var content = options.label.content;
                if (options.label.url != "") {
                    content = new EJS({ url: options.label.url, cache: false }).render(options.data);
                }
                if (options.data.Address) {
                    if (options.data.Address.length > 19) {
                        options.label.offset.h -= 20;
                    }
                }
                var label = new BMap.Label(content, { offset: new BMap.Size(options.label.offset.w, options.label.offset.h), position: point });
                if (options.label.customStyle != undefined) {

                } else {
                    label.setStyle({ fontSize: "", whiteSpace: "", fontStyle: "", fontVariant: "", fontWeight: "", fontFamily: "" });
                }
                map.addOverlay(label);
            }
            if (typeof (options.callback) === "function") {
                options.callback();
            }
        }
    };
})(jQuery);